var boardStatus = new Status();

$(document).ready(function(){
	
	// Event listener
	$("#A1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#A8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#A9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#B1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#B8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#B9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#C1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#C8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#C9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#D1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#D8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#D9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#E1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#E8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#E9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#F1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#F8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#F9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#G1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#G8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#G9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#H1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#H8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#H9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	$("#J1").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J2").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J3").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J4").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J5").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J6").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J7").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	$("#J8").click(function(){ placeStone($(this),boardStatus.nextTurn); });	
	$("#J9").click(function(){ placeStone($(this),boardStatus.nextTurn); });
	
	// Incoming Hangout delta
	gapi.hangout.data.onStateChanged.add( function(event){
		console.log("Receiving delta");
		// reading boardStatus
		var state = gapi.hangout.data.getState();
		var delta = state.Status;
		console.log(state);
		var exdelta = JSON.parse(delta);
		boardStatus = exdelta;
		
		// redraw board
		drawBoard();
		
		// update nextTurn
		if(boardStatus.nextTurn == "black"){
			$("#NextBlack").addClass("selected");
			$("#NextWhite").removeClass("selected");
		}else{
			$("#NextBlack").removeClass("selected");
			$("#NextWhite").addClass("selected");
		}
	});
	
});

function placeStone(location, colour){
	
	// place a stone on the board
	
	var coor = location.attr("id");
	var mapped_coor = CoorMapping(coor);
	
	$("#console").text("Last played at: "+coor);
	if(boardStatus.boardMap[mapped_coor[0]][mapped_coor[1]]==0){
		if(boardStatus.nextTurn == "black"){
			boardStatus.boardMap[mapped_coor[0]][mapped_coor[1]] = 1;
		}else{
			boardStatus.boardMap[mapped_coor[0]][mapped_coor[1]] = -1;
		}
		
		var legal = analyseBoard(mapped_coor);
		if( legal == 1){
			drawBoard();
			if(boardStatus.nextTurn == "black"){
				boardStatus.lastBlackMoved = [mapped_coor[0],mapped_coor[1]];
			}else{
				boardStatus.lastWhiteMoved = [mapped_coor[0],mapped_coor[1]];
			}
		}else{
			$("#console").text("Illegal move");
			return;
		}
		
		// update nextTurn
		if(boardStatus.nextTurn == "black"){
			boardStatus.nextTurn = "white";
		}else{
			boardStatus.nextTurn = "black";
		}
		
		// send a Hangout delta
		console.log("Sending delta");
		var delta = JSON.stringify(boardStatus);
		gapi.hangout.data.setValue("Status",delta);
	}
	
}

function analyseBoard(lastMove){
	boardLabel = [[0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0]];
	
	var nextLabel = 1;
	var equiv = new Array();
	
	// create connected-component map
	//first pass
	for (var i=0; i<9; i++){
		for (var j=0; j<9; j++){
			var value = boardStatus.boardMap[i][j];
			var label = 0;
			if(value != 0){
				var west = 0;
				if( j>0 ){ west = boardStatus.boardMap[i][j-1]; }
				var north = 0;
				if( i>0 ){ north = boardStatus.boardMap[i-1][j];}
				
				//condition 1
				if (west == value){
					label = boardLabel[i][j-1];
				}
				//condition 2
				if ((west == value)&&(north == value)){
					if (boardLabel[i-1][j] < boardLabel[i][j-1]){
						label = boardLabel[i-1][j];
						equiv.push([ boardLabel[i][j-1] , boardLabel[i-1][j] ]);
					}else{
						label = boardLabel[i][j-1];
						equiv.push([ boardLabel[i-1][j] , boardLabel[i][j-1] ]);
					}
				}
				//condition 3
				if ((west != value)&&(north == value)){
					label = boardLabel[i-1][j];
				}
				//condition 4
				if ((west != value)&&(north != value)){
					label = nextLabel;
					nextLabel++;
				}
				boardLabel[i][j] = label;
			}
		}
	}	
	
	// second pass
	for (var i=0; i<9; i++){
		for (var j=0; j<9; j++){
			for (var k=0;k<equiv.length;k++){
				if ( boardLabel[i][j] == equiv[k][0] ){
					boardLabel[i][j] = equiv[k][1];
				}
			}
		}
	}
	
	//console.log(boardLabel);
	
	// counting breath of each blob
	var breathCount = new Array();
	for (var i=0;i<9;i++){
		for (var j=0;j<9;j++){
			var label = boardLabel[i][j];
			if (label != 0){
				var exist = 0;
				var index = -1;
				for(var k=0; k<breathCount.length; k++){
					if (breathCount[k][0] == label){ exist = 1; index = k; }
				}
				if(exist == 1){
					var breath = breathCount[index][1];
					var value = boardStatus.boardMap[i][j];
					var west = value;
					if(j>0){ west = boardStatus.boardMap[i][j-1];}
					var north = value;
					if(i>0){ north = boardStatus.boardMap[i-1][j];}
					var east = value;
					if(j<8){ east = boardStatus.boardMap[i][j+1];}
					var south = value;
					if(i<8){ south = boardStatus.boardMap[i+1][j];}
					
					if(west == 0){ 
						var registered = 0;
						for(var k=0;k<breath.length;k++){
							if((breath[k][0] == i)&&(breath[k][1]==j-1)){
								registered = 1;
							}
						}
						if (registered == 0){
							breath.push([i,j-1]);
						}
					}
					if(north == 0){ 
						var registered = 0;
						for(var k=0;k<breath.length;k++){
							if((breath[k][0] == i-1)&&(breath[k][1]==j)){
								registered = 1;
							}
						}
						if (registered == 0){
							breath.push([i-1,j]);
						}
					}
					if(east == 0){ 
						var registered = 0;
						for(var k=0;k<breath.length;k++){
							if((breath[k][0] == i)&&(breath[k][1]==j+1)){
								registered = 1;
							}
						}
						if (registered == 0){
							breath.push([i,j+1]);
						}						
					}
					if(south == 0){
						var registered = 0;
						for(var k=0;k<breath.length;k++){
							if((breath[k][0] == i+1)&&(breath[k][1]==j)){
								registered = 1;
							}
						}
						if (registered == 0){
							breath.push([i+1,j]);
						}	
					}
					
					breathCount.splice(index,1,[label, breath]);
					
				}else{
					var breath = new Array();
					var value = boardStatus.boardMap[i][j];
					var west = value;
					if(j>0){ west = boardStatus.boardMap[i][j-1];}
					var north = value;
					if(i>0){ north = boardStatus.boardMap[i-1][j];}
					var east = value;
					if(j<8){ east = boardStatus.boardMap[i][j+1];}
					var south = value;
					if(i<8){ south = boardStatus.boardMap[i+1][j];}
					
					if(west == 0){ breath.push([i,j-1]); }
					if(north == 0){ breath.push([i-1,j]); }
					if(east == 0){ breath.push([i,j+1]); }
					if(south == 0){ breath.push([i+1,j]); }
					
					breathCount.push([label, breath]);
				}
			}
		}
	}
	
	//console.log(breathCount);
	
	// perform capturing
	var placedLabel = boardLabel[lastMove[0]][lastMove[1]];
	var successfulAttack = 0;
	for (var i=0;i<9;i++){
		for (var j=0;j<9;j++){
			
			var label = boardLabel[i][j];
			if((label!=0)&&(label!=placedLabel)){
				var exist = 0;
				var index = -1;
				for(var k=0; k<breathCount.length; k++){
					if (breathCount[k][0] == label){ exist = 1; index = k; }
				}
				if( breathCount[index][1]==0 ){
					if (boardStatus.boardMap[i][j]>0){
						boardStatus.blackCapture++;
						boardStatus.boardMap[i][j] = 0;
					}else{
						boardStatus.whiteCapture++;
						boardStatus.boardMap[i][j] = 0;
					}
					successfulAttack = 1;
				}
			}
			
		}
	}
	if( successfulAttack == 0){
		var exist = 0;
		var index = -1;
		for(var k=0; k<breathCount.length; k++){
			if (breathCount[k][0] == placedLabel){ exist = 1; index = k; }
		}
		if( breathCount[index][1]==0 ){
			boardStatus.boardMap[lastMove[0]][lastMove[1]] = 0;
			return 0;
		}
	}
	
	return 1;
}

function drawBoard(){

	$("#A1").removeClass("black white");
	if(boardStatus.boardMap[0][0]>0){
		$("#A1").addClass("black");
	}else if(boardStatus.boardMap[0][0]<0){
		$("#A1").addClass("white");}
	$("#A2").removeClass("black white");
	if(boardStatus.boardMap[0][1]>0){
		$("#A2").addClass("black");
	}else if(boardStatus.boardMap[0][1]<0){
		$("#A2").addClass("white");}
	$("#A3").removeClass("black white");
	if(boardStatus.boardMap[0][2]>0){
		$("#A3").addClass("black");
	}else if(boardStatus.boardMap[0][2]<0){
		$("#A3").addClass("white");}
	$("#A4").removeClass("black white");
	if(boardStatus.boardMap[0][3]>0){
		$("#A4").addClass("black");
	}else if(boardStatus.boardMap[0][3]<0){
		$("#A4").addClass("white");}
	$("#A5").removeClass("black white");
	if(boardStatus.boardMap[0][4]>0){
		$("#A5").addClass("black");
	}else if(boardStatus.boardMap[0][4]<0){
		$("#A5").addClass("white");}
	$("#A6").removeClass("black white");
	if(boardStatus.boardMap[0][5]>0){
		$("#A6").addClass("black");
	}else if(boardStatus.boardMap[0][5]<0){
		$("#A6").addClass("white");}
	$("#A7").removeClass("black white");
	if(boardStatus.boardMap[0][6]>0){
		$("#A7").addClass("black");
	}else if(boardStatus.boardMap[0][6]<0){
		$("#A7").addClass("white");}
	$("#A8").removeClass("black white");
	if(boardStatus.boardMap[0][7]>0){
		$("#A8").addClass("black");
	}else if(boardStatus.boardMap[0][7]<0){
		$("#A8").addClass("white");}
	$("#A9").removeClass("black white");
	if(boardStatus.boardMap[0][8]>0){
		$("#A9").addClass("black");
	}else if(boardStatus.boardMap[0][8]<0){
		$("#A9").addClass("white");}
		
	$("#B1").removeClass("black white");
	if(boardStatus.boardMap[1][0]>0){
		$("#B1").addClass("black");
	}else if(boardStatus.boardMap[1][0]<0){
		$("#B1").addClass("white");}
	$("#B2").removeClass("black white");
	if(boardStatus.boardMap[1][1]>0){
		$("#B2").addClass("black");
	}else if(boardStatus.boardMap[1][1]<0){
		$("#B2").addClass("white");}
	$("#B3").removeClass("black white");
	if(boardStatus.boardMap[1][2]>0){
		$("#B3").addClass("black");
	}else if(boardStatus.boardMap[1][2]<0){
		$("#B3").addClass("white");}
	$("#B4").removeClass("black white");
	if(boardStatus.boardMap[1][3]>0){
		$("#B4").addClass("black");
	}else if(boardStatus.boardMap[1][3]<0){
		$("#B4").addClass("white");}
	$("#B5").removeClass("black white");
	if(boardStatus.boardMap[1][4]>0){
		$("#B5").addClass("black");
	}else if(boardStatus.boardMap[1][4]<0){
		$("#B5").addClass("white");}
	$("#B6").removeClass("black white");
	if(boardStatus.boardMap[1][5]>0){
		$("#B6").addClass("black");
	}else if(boardStatus.boardMap[1][5]<0){
		$("#B6").addClass("white");}
	$("#B7").removeClass("black white");
	if(boardStatus.boardMap[1][6]>0){
		$("#B7").addClass("black");
	}else if(boardStatus.boardMap[1][6]<0){
		$("#B7").addClass("white");}
	$("#B8").removeClass("black white");
	if(boardStatus.boardMap[1][7]>0){
		$("#B8").addClass("black");
	}else if(boardStatus.boardMap[1][7]<0){
		$("#B8").addClass("white");}
	$("#B9").removeClass("black white");
	if(boardStatus.boardMap[1][8]>0){
		$("#B9").addClass("black");
	}else if(boardStatus.boardMap[1][8]<0){
		$("#B9").addClass("white");}
		
	$("#C1").removeClass("black white");
	if(boardStatus.boardMap[2][0]>0){
		$("#C1").addClass("black");
	}else if(boardStatus.boardMap[2][0]<0){
		$("#C1").addClass("white");}
	$("#C2").removeClass("black white");
	if(boardStatus.boardMap[2][1]>0){
		$("#C2").addClass("black");
	}else if(boardStatus.boardMap[2][1]<0){
		$("#C2").addClass("white");}
	$("#C3").removeClass("black white");
	if(boardStatus.boardMap[2][2]>0){
		$("#C3").addClass("black");
	}else if(boardStatus.boardMap[2][2]<0){
		$("#C3").addClass("white");}
	$("#C4").removeClass("black white");
	if(boardStatus.boardMap[2][3]>0){
		$("#C4").addClass("black");
	}else if(boardStatus.boardMap[2][3]<0){
		$("#C4").addClass("white");}
	$("#C5").removeClass("black white");
	if(boardStatus.boardMap[2][4]>0){
		$("#C5").addClass("black");
	}else if(boardStatus.boardMap[2][4]<0){
		$("#C5").addClass("white");}
	$("#C6").removeClass("black white");
	if(boardStatus.boardMap[2][5]>0){
		$("#C6").addClass("black");
	}else if(boardStatus.boardMap[2][5]<0){
		$("#C6").addClass("white");}
	$("#C7").removeClass("black white");
	if(boardStatus.boardMap[2][6]>0){
		$("#C7").addClass("black");
	}else if(boardStatus.boardMap[2][6]<0){
		$("#C7").addClass("white");}
	$("#C8").removeClass("black white");
	if(boardStatus.boardMap[2][7]>0){
		$("#C8").addClass("black");
	}else if(boardStatus.boardMap[2][7]<0){
		$("#C8").addClass("white");}
	$("#C9").removeClass("black white");
	if(boardStatus.boardMap[2][8]>0){
		$("#C9").addClass("black");
	}else if(boardStatus.boardMap[2][8]<0){
		$("#C9").addClass("white");}
		
	$("#D1").removeClass("black white");
	if(boardStatus.boardMap[3][0]>0){
		$("#D1").addClass("black");
	}else if(boardStatus.boardMap[3][0]<0){
		$("#D1").addClass("white");}
	$("#D2").removeClass("black white");
	if(boardStatus.boardMap[3][1]>0){
		$("#D2").addClass("black");
	}else if(boardStatus.boardMap[3][1]<0){
		$("#D2").addClass("white");}
	$("#D3").removeClass("black white");
	if(boardStatus.boardMap[3][2]>0){
		$("#D3").addClass("black");
	}else if(boardStatus.boardMap[3][2]<0){
		$("#D3").addClass("white");}
	$("#D4").removeClass("black white");
	if(boardStatus.boardMap[3][3]>0){
		$("#D4").addClass("black");
	}else if(boardStatus.boardMap[3][3]<0){
		$("#D4").addClass("white");}
	$("#D5").removeClass("black white");
	if(boardStatus.boardMap[3][4]>0){
		$("#D5").addClass("black");
	}else if(boardStatus.boardMap[3][4]<0){
		$("#D5").addClass("white");}
	$("#D6").removeClass("black white");
	if(boardStatus.boardMap[3][5]>0){
		$("#D6").addClass("black");
	}else if(boardStatus.boardMap[3][5]<0){
		$("#D6").addClass("white");}
	$("#D7").removeClass("black white");
	if(boardStatus.boardMap[3][6]>0){
		$("#D7").addClass("black");
	}else if(boardStatus.boardMap[3][6]<0){
		$("#D7").addClass("white");}
	$("#D8").removeClass("black white");
	if(boardStatus.boardMap[3][7]>0){
		$("#D8").addClass("black");
	}else if(boardStatus.boardMap[3][7]<0){
		$("#D8").addClass("white");}
	$("#D9").removeClass("black white");
	if(boardStatus.boardMap[3][8]>0){
		$("#D9").addClass("black");
	}else if(boardStatus.boardMap[3][8]<0){
		$("#D9").addClass("white");}
		
	$("#E1").removeClass("black white");
	if(boardStatus.boardMap[4][0]>0){
		$("#E1").addClass("black");
	}else if(boardStatus.boardMap[4][0]<0){
		$("#E1").addClass("white");}
	$("#E2").removeClass("black white");
	if(boardStatus.boardMap[4][1]>0){
		$("#E2").addClass("black");
	}else if(boardStatus.boardMap[4][1]<0){
		$("#E2").addClass("white");}
	$("#E3").removeClass("black white");
	if(boardStatus.boardMap[4][2]>0){
		$("#E3").addClass("black");
	}else if(boardStatus.boardMap[4][2]<0){
		$("#E3").addClass("white");}
	$("#E4").removeClass("black white");
	if(boardStatus.boardMap[4][3]>0){
		$("#E4").addClass("black");
	}else if(boardStatus.boardMap[4][3]<0){
		$("#E4").addClass("white");}
	$("#E5").removeClass("black white");
	if(boardStatus.boardMap[4][4]>0){
		$("#E5").addClass("black");
	}else if(boardStatus.boardMap[4][4]<0){
		$("#E5").addClass("white");}
	$("#E6").removeClass("black white");
	if(boardStatus.boardMap[4][5]>0){
		$("#E6").addClass("black");
	}else if(boardStatus.boardMap[4][5]<0){
		$("#E6").addClass("white");}
	$("#E7").removeClass("black white");
	if(boardStatus.boardMap[4][6]>0){
		$("#E7").addClass("black");
	}else if(boardStatus.boardMap[4][6]<0){
		$("#E7").addClass("white");}
	$("#E8").removeClass("black white");
	if(boardStatus.boardMap[4][7]>0){
		$("#E8").addClass("black");
	}else if(boardStatus.boardMap[4][7]<0){
		$("#E8").addClass("white");}
	$("#E9").removeClass("black white");
	if(boardStatus.boardMap[4][8]>0){
		$("#E9").addClass("black");
	}else if(boardStatus.boardMap[4][8]<0){
		$("#E9").addClass("white");}
		
	$("#F1").removeClass("black white");
	if(boardStatus.boardMap[5][0]>0){
		$("#F1").addClass("black");
	}else if(boardStatus.boardMap[5][0]<0){
		$("#F1").addClass("white");}
	$("#F2").removeClass("black white");
	if(boardStatus.boardMap[5][1]>0){
		$("#F2").addClass("black");
	}else if(boardStatus.boardMap[5][1]<0){
		$("#F2").addClass("white");}
	$("#F3").removeClass("black white");
	if(boardStatus.boardMap[5][2]>0){
		$("#F3").addClass("black");
	}else if(boardStatus.boardMap[5][2]<0){
		$("#F3").addClass("white");}
	$("#F4").removeClass("black white");
	if(boardStatus.boardMap[5][3]>0){
		$("#F4").addClass("black");
	}else if(boardStatus.boardMap[5][3]<0){
		$("#F4").addClass("white");}
	$("#F5").removeClass("black white");
	if(boardStatus.boardMap[5][4]>0){
		$("#F5").addClass("black");
	}else if(boardStatus.boardMap[5][4]<0){
		$("#F5").addClass("white");}
	$("#F6").removeClass("black white");
	if(boardStatus.boardMap[5][5]>0){
		$("#F6").addClass("black");
	}else if(boardStatus.boardMap[5][5]<0){
		$("#F6").addClass("white");}
	$("#F7").removeClass("black white");
	if(boardStatus.boardMap[5][6]>0){
		$("#F7").addClass("black");
	}else if(boardStatus.boardMap[5][6]<0){
		$("#F7").addClass("white");}
	$("#F8").removeClass("black white");
	if(boardStatus.boardMap[5][7]>0){
		$("#F8").addClass("black");
	}else if(boardStatus.boardMap[5][7]<0){
		$("#F8").addClass("white");}
	$("#F9").removeClass("black white");
	if(boardStatus.boardMap[5][8]>0){
		$("#F9").addClass("black");
	}else if(boardStatus.boardMap[5][8]<0){
		$("#F9").addClass("white");}
		
	$("#G1").removeClass("black white");
	if(boardStatus.boardMap[6][0]>0){
		$("#G1").addClass("black");
	}else if(boardStatus.boardMap[6][0]<0){
		$("#G1").addClass("white");}
	$("#G2").removeClass("black white");
	if(boardStatus.boardMap[6][1]>0){
		$("#G2").addClass("black");
	}else if(boardStatus.boardMap[6][1]<0){
		$("#G2").addClass("white");}
	$("#G3").removeClass("black white");
	if(boardStatus.boardMap[6][2]>0){
		$("#G3").addClass("black");
	}else if(boardStatus.boardMap[6][2]<0){
		$("#G3").addClass("white");}
	$("#G4").removeClass("black white");
	if(boardStatus.boardMap[6][3]>0){
		$("#G4").addClass("black");
	}else if(boardStatus.boardMap[6][3]<0){
		$("#G4").addClass("white");}
	$("#G5").removeClass("black white");
	if(boardStatus.boardMap[6][4]>0){
		$("#G5").addClass("black");
	}else if(boardStatus.boardMap[6][4]<0){
		$("#G5").addClass("white");}
	$("#G6").removeClass("black white");
	if(boardStatus.boardMap[6][5]>0){
		$("#G6").addClass("black");
	}else if(boardStatus.boardMap[6][5]<0){
		$("#G6").addClass("white");}
	$("#G7").removeClass("black white");
	if(boardStatus.boardMap[6][6]>0){
		$("#G7").addClass("black");
	}else if(boardStatus.boardMap[6][6]<0){
		$("#G7").addClass("white");}
	$("#G8").removeClass("black white");
	if(boardStatus.boardMap[6][7]>0){
		$("#G8").addClass("black");
	}else if(boardStatus.boardMap[6][7]<0){
		$("#G8").addClass("white");}
	$("#G9").removeClass("black white");
	if(boardStatus.boardMap[6][8]>0){
		$("#G9").addClass("black");
	}else if(boardStatus.boardMap[6][8]<0){
		$("#G9").addClass("white");}
		
	$("#H1").removeClass("black white");
	if(boardStatus.boardMap[7][0]>0){
		$("#H1").addClass("black");
	}else if(boardStatus.boardMap[7][0]<0){
		$("#H1").addClass("white");}
	$("#H2").removeClass("black white");
	if(boardStatus.boardMap[7][1]>0){
		$("#H2").addClass("black");
	}else if(boardStatus.boardMap[7][1]<0){
		$("#H2").addClass("white");}
	$("#H3").removeClass("black white");
	if(boardStatus.boardMap[7][2]>0){
		$("#H3").addClass("black");
	}else if(boardStatus.boardMap[7][2]<0){
		$("#H3").addClass("white");}
	$("#H4").removeClass("black white");
	if(boardStatus.boardMap[7][3]>0){
		$("#H4").addClass("black");
	}else if(boardStatus.boardMap[7][3]<0){
		$("#H4").addClass("white");}
	$("#H5").removeClass("black white");
	if(boardStatus.boardMap[7][4]>0){
		$("#H5").addClass("black");
	}else if(boardStatus.boardMap[7][4]<0){
		$("#H5").addClass("white");}
	$("#H6").removeClass("black white");
	if(boardStatus.boardMap[7][5]>0){
		$("#H6").addClass("black");
	}else if(boardStatus.boardMap[7][5]<0){
		$("#H6").addClass("white");}
	$("#H7").removeClass("black white");
	if(boardStatus.boardMap[7][6]>0){
		$("#H7").addClass("black");
	}else if(boardStatus.boardMap[7][6]<0){
		$("#H7").addClass("white");}
	$("#H8").removeClass("black white");
	if(boardStatus.boardMap[7][7]>0){
		$("#H8").addClass("black");
	}else if(boardStatus.boardMap[7][7]<0){
		$("#H8").addClass("white");}
	$("#H9").removeClass("black white");
	if(boardStatus.boardMap[7][8]>0){
		$("#H9").addClass("black");
	}else if(boardStatus.boardMap[7][8]<0){
		$("#H9").addClass("white");}
		
	$("#J1").removeClass("black white");
	if(boardStatus.boardMap[8][0]>0){
		$("#J1").addClass("black");
	}else if(boardStatus.boardMap[8][0]<0){
		$("#J1").addClass("white");}
	$("#J2").removeClass("black white");
	if(boardStatus.boardMap[8][1]>0){
		$("#J2").addClass("black");
	}else if(boardStatus.boardMap[8][1]<0){
		$("#J2").addClass("white");}
	$("#J3").removeClass("black white");
	if(boardStatus.boardMap[8][2]>0){
		$("#J3").addClass("black");
	}else if(boardStatus.boardMap[8][2]<0){
		$("#J3").addClass("white");}
	$("#J4").removeClass("black white");
	if(boardStatus.boardMap[8][3]>0){
		$("#J4").addClass("black");
	}else if(boardStatus.boardMap[8][3]<0){
		$("#J4").addClass("white");}
	$("#J5").removeClass("black white");
	if(boardStatus.boardMap[8][4]>0){
		$("#J5").addClass("black");
	}else if(boardStatus.boardMap[8][4]<0){
		$("#J5").addClass("white");}
	$("#J6").removeClass("black white");
	if(boardStatus.boardMap[8][5]>0){
		$("#J6").addClass("black");
	}else if(boardStatus.boardMap[8][5]<0){
		$("#J6").addClass("white");}
	$("#J7").removeClass("black white");
	if(boardStatus.boardMap[8][6]>0){
		$("#J7").addClass("black");
	}else if(boardStatus.boardMap[8][6]<0){
		$("#J7").addClass("white");}
	$("#J8").removeClass("black white");
	if(boardStatus.boardMap[8][7]>0){
		$("#J8").addClass("black");
	}else if(boardStatus.boardMap[8][7]<0){
		$("#J8").addClass("white");}
	$("#J9").removeClass("black white");
	if(boardStatus.boardMap[8][8]>0){
		$("#J9").addClass("black");
	}else if(boardStatus.boardMap[8][8]<0){
		$("#J9").addClass("white");}
}

function CoorMapping(coordinate){
	var mapped_coor = [-1, -1];
	switch(coordinate[0]){
		case 'A': mapped_coor[0]=0; break;
		case 'B': mapped_coor[0]=1; break;
		case 'C': mapped_coor[0]=2; break;
		case 'D': mapped_coor[0]=3; break;
		case 'E': mapped_coor[0]=4; break;
		case 'F': mapped_coor[0]=5; break;
		case 'G': mapped_coor[0]=6; break;
		case 'H': mapped_coor[0]=7; break;
		case 'J': mapped_coor[0]=8; break;
		default : mapped_coor[0]=-1;
	}
	
	switch(coordinate[1]){
		case '1': mapped_coor[1]=0; break;
		case '2': mapped_coor[1]=1; break;
		case '3': mapped_coor[1]=2; break;
		case '4': mapped_coor[1]=3; break;
		case '5': mapped_coor[1]=4; break;
		case '6': mapped_coor[1]=5; break;
		case '7': mapped_coor[1]=6; break;
		case '8': mapped_coor[1]=7; break;
		case '9': mapped_coor[1]=8; break;
		default : mapped_coor[1]=-1;
	}
	
	return mapped_coor;
}

function Status(){
	this.nextTurn = "black";
	this.boardMap = new Array();
	this.boardMap = [[0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0],
	     [0,0,0,0,0,0,0,0,0]];
	this.whiteCaptured = 0;
	this.blackCaptured = 0;
	this.lastWhiteMoved = [-1,-1];
	this.lastBlackMoved = [-1,-1];
}

function init() {
  // When API is ready...                                                         

}

// Wait for gadget to load.                                                       
gadgets.util.registerOnLoadHandler(init);